import json
from langchain_deepseek import ChatDeepSeek
from langgraph.prebuilt import create_react_agent
from langchain_mcp_adapters.client import MultiServerMCPClient


# ========================= AgentGenerator类 - 生成Agent =========================
class AgentGenerator:

    def __init__(self) -> None:
        pass

    async def load_tools(self, servers_config_path):
        with open(servers_config_path, "r", encoding="utf-8") as f:
            mcp_servers = json.load(f).get("mcp_servers", {})
        mcp_client = MultiServerMCPClient(mcp_servers)
        return await mcp_client.get_tools()

    # 创建 LangGraph Agent
    async def create_prebuilt_agent(self, model_name, prompts_file_path, servers_config_path):
        # 加载大语言模型
        model = ChatDeepSeek(model=model_name)
        # 加载提示词
        with open(prompts_file_path, "r", encoding="utf-8") as f:
            prompt = f.read()
        # 加载工具列表
        tools = await self.load_tools(servers_config_path)
        return create_react_agent(model=model, tools=tools, prompt=prompt)
